<template>
  <!-- 添加或修改报文交换记录对话框 -->
  <el-dialog :title="title" :visible.sync="dialogVisible" width="1000px" append-to-body :close-on-click-modal="false"
    @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="98px" label-position="right" class="flex-form"
      size="small">
      <el-form-item label="记录类型" prop="recordType">
        <el-select v-model.trim="form.recordType" placeholder="请选择记录类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>

      <el-form-item label="报文ID" prop="messageId">
        <el-input v-model.trim="form.messageId" placeholder="请输入报文ID" />
      </el-form-item>

      <el-form-item label="功能代码" prop="functionCode">
        <el-input v-model.trim="form.functionCode" placeholder="请输入功能代码" />
      </el-form-item>

      <el-form-item label="报文类型" prop="messageType">
        <el-input v-model.trim="form.messageType" placeholder="请输入报文类型" />
      </el-form-item>

      <el-form-item label="发送者ID" prop="sendId">
        <el-input v-model.trim="form.sendId" placeholder="请输入发送者ID" />
      </el-form-item>

      <el-form-item label="接受者ID" prop="receiverId">
        <el-input v-model.trim="form.receiverId" placeholder="请输入接受者ID" />
      </el-form-item>

      <el-form-item label="版本" prop="version">
        <el-input v-model.trim="form.version" placeholder="请输入版本" />
      </el-form-item>

      <el-form-item label="原始报文" prop="originText">
        <el-input v-model.trim="form.originText" type="textarea" placeholder="请输入内容" />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    getMftMessageExchageRecord,
    addMftMessageExchageRecord,
    updateMftMessageExchageRecord
  } from "@/api/swdecl/mftMessageExchageRecord";
  export default {
    name: "MftMessageExchageRecord-addEdit",
    props: {
      dialogType: {
        type: String,
        default: "add",
      },
      rowData: {
        type: Object,
        default: () => {},
      },
      dict: {}
    },
    data() {
      return {
        // 弹出层
        dialogVisible: false,
        // 标题
        title: "新增",
        // 表单参数
        form: {
          id: null,
          recordType: null,
          messageId: null,
          functionCode: null,
          messageType: null,
          sendId: null,
          receiverId: null,
          version: null,
          originText: null

        },
        // 报文交换记录表格数据
        mftMessageExchageRecordList: [],
        // 表单校验
        rules: {
          id: [{
            required: true,
            message: "主键不能为空",
            trigger: "blur"
          }],
          recordType: [{
            required: true,
            message: "记录类型不能为空",
            trigger: "change"
          }],
          messageId: [{
            required: true,
            message: "报文ID不能为空",
            trigger: "blur"
          }],
          functionCode: [{
            required: true,
            message: "功能代码不能为空",
            trigger: "blur"
          }],
          messageType: [{
            required: true,
            message: "报文类型不能为空",
            trigger: "blur"
          }],
          sendId: [{
            required: true,
            message: "发送者ID不能为空",
            trigger: "blur"
          }],
          receiverId: [{
            required: true,
            message: "接受者ID不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    methods: {
      open() {
        this.dialogVisible = true;
        this.$nextTick(() => {
          if (this.dialogType == "add") {
            this.title = "新增";
          } else if (this.dialogType == "edit") {
            this.title = "编辑";
            this.loadDetail();
          } else if (this.dialogType == "detail") {
            this.title = "详情";
            this.loadDetail();
          }
        });
      },
      close() {
        this.$refs["form"].resetFields();
        this.dialogVisible = false;
      },

      // 获取详情
      loadDetail() {
        const id = this.rowData.id;
        getMftMessageExchageRecord(id).then((res) => {
          if (res.code == 200) {
            this.form = res.data;
          }
        });
      },
      // 提交表单
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let params = {
              ...this.form,
            };
            if (this.dialogType == "add") {
              addMftMessageExchageRecord(this.form).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("新增成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            } else {
              params.id = this.rowData.id;
              updateMftMessageExchageRecord(this.form).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("修改成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            }
          }
        });
      },
    },
  };
</script>

<style lang="scss" scoped></style>
